# Network adapter descriptions and names mapping
$adapterDescriptions = @{
    "adapter1" = "Realtek PCIe GbE Family Controller"
    "adapter2" = "Realtek PCIe GBE Family Controller"
}
$adapterNames = @{
    "adapter1" = "net1"
    "adapter2" = "net2"
}
# Speed threshold in kb/s
$thresholdSpeed = 1024  # 1M/s in kb/s
# Speed check interval in seconds
$speedCheckDuration = 5
# Duration to confirm low speed in seconds
$maxLowSpeedDurationCount = 3  # Number of checks before action (15 seconds at 5s interval)

# State machine variables
$currentAdapterDesc = $adapterDescriptions["Wireless"]
$currentAdapterName = $adapterNames[$currentAdapterDesc]
$speedBelowThresholdCount = 0
$state = "WaitingToReceiveFile"

# Function to get current adapter speed
function Get-CurrentAdapterSpeed {
    param([string]$adapterName)
    $adapter = Get-NetAdapterStatistics -Name $adapterName
    return @{ReceivedSpeed = [Math]::Round($adapter.ReceivedBytes / 1024); SentSpeed = [Math]::Round($adapter.SentBytes / 1024) }
}

# Function to switch adapters based on state
function Switch-AdaptersIfNecessary {
    global $currentAdapterDesc, $currentAdapterName, $speedBelowThresholdCount, $maxLowSpeedDurationCount, $state

    if ($currentAdapterDesc -eq $adapterDescriptions["adapter2"]) {
        $nextAdapterDesc = $adapterDescriptions["adapter1"]
    }
    else {
        $nextAdapterDesc = $adapterDescriptions["adapter2"]
    }
    
    $nextAdapterName = $adapterNames[$nextAdapterDesc]

    if ($speedBelowThresholdCount -ge $maxLowSpeedDurationCount) {
        Write-Host "$(Get-Date) - Switching from $currentAdapterDesc to $nextAdapterDesc due to sustained low speed."
        Disable-NetAdapter -Name $currentAdapterName -Confirm:$false
        Enable-NetAdapter -Name $nextAdapterName -Confirm:$false
        $currentAdapterDesc = $nextAdapterDesc
        $currentAdapterName = $nextAdapterName
        $speedBelowThresholdCount = 0
        
        # State transitions
        if ($state -eq "ReceivingFile") {
            $state = "FileReceived"
        }
        elseif ($state -eq "UploadingFile") {
            $state = "FileUploaded"
        }
    }
}

# Initial setup
Disable-NetAdapter -Name $adapterNames["adapter1"] -Confirm:$false
# Enable-NetAdapter -Name $currentAdapterName -Confirm:$false
Enable-NetAdapter -Name $adapterNames["adapter2"] -Confirm:$false

# Main monitoring loop
while ($true) {
    $speeds = Get-CurrentAdapterSpeed -adapterName $currentAdapterName
    $receivedSpeed = $speeds.ReceivedSpeed
    $sentSpeed = $speeds.SentSpeed
    Write-Host "$(Get-Date) - Current received speed on $($currentAdapterDesc): $receivedSpeed kb/s, sent speed: $sentSpeed kb/s"

    # State machine to manage the state transitions
    switch ($state) {
        "WaitingToReceiveFile" {
            if ($receivedSpeed -gt $thresholdSpeed) {
                Write-Host "Switching to ReceivingFile state"
                $state = "ReceivingFile"
                $speedBelowThresholdCount = 0
            }
        }
        "ReceivingFile" {
            if ($receivedSpeed -lt $thresholdSpeed) {
                $speedBelowThresholdCount++
                Write-Host "$(Get-Date) - Speed below threshold. Count: $speedBelowThresholdCount"
            }
            else {
                $speedBelowThresholdCount = 0
            }
            Switch-AdaptersIfNecessary
        }
        "FileReceived" {
            Write-Host "Switching to WaitingToUploadFile state"
            $state = "WaitingToUploadFile"
        }
        "WaitingToUploadFile" {
            if ($sentSpeed -gt $thresholdSpeed) {
                Write-Host "Switching to UploadingFile state"
                $state = "UploadingFile"
                $speedBelowThresholdCount = 0
            }
        }
        "UploadingFile" {
            if ($sentSpeed -lt $thresholdSpeed) {
                $speedBelowThresholdCount++
                Write-Host "$(Get-Date) - Speed below threshold. Count: $speedBelowThresholdCount"
            }
            else {
                $speedBelowThresholdCount = 0
            }
            Switch-AdaptersIfNecessary
        }
        "FileUploaded" {
            Write-Host "Switching to WaitingToReceiveFile state"
            $state = "WaitingToReceiveFile"
        }
    }

    Start-Sleep -Seconds $speedCheckDuration
}
